/*
 * Copyright 2010 EES GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.eesgmbh.gimv.samples.googlechartapi.client;

import java.util.Date;

import org.eesgmbh.gimv.client.event.SetDomainBoundsEvent;
import org.eesgmbh.gimv.client.event.SetDomainBoundsEventHandler;
import org.eesgmbh.gimv.shared.util.Bound;

import com.google.gwt.event.shared.HandlerManager;

public class CustomCalendarPresenter {

	public interface View {
		void setDateString(String date);
		void hideCalendar();
	}

	private final CustomCalendarPresenter.View view;
	private Bound boundToSet;

	public CustomCalendarPresenter(HandlerManager handlerManager, CustomCalendarPresenter.View view) {
		this.view = view;

		CustomCalendarPresenterEventHandler eventHandler = new CustomCalendarPresenterEventHandler();

		handlerManager.addHandler(SetDomainBoundsEvent.TYPE, eventHandler);
	}

	public void configureBound(Bound bound) {
		this.boundToSet = bound;
	}

	private void onSetDomainBounds(SetDomainBoundsEvent event) {
		if (boundToSet == Bound.LEFT) {
			this.view.setDateString(new Date(event.getBounds().getLeft().longValue()).toLocaleString());
		} else if (boundToSet == Bound.RIGHT) {
			this.view.setDateString(new Date(event.getBounds().getRight().longValue()).toLocaleString());
		}

		// for now, lets take this shortcut to close the calendar popup
		this.view.hideCalendar();
	}

	private class CustomCalendarPresenterEventHandler implements SetDomainBoundsEventHandler {

		public void onSetDomainBounds(SetDomainBoundsEvent event) {
			CustomCalendarPresenter.this.onSetDomainBounds(event);
		}

	}
}
